# -*- coding: utf-8 -*-
import streamlit as st
import requests
import time

st.title("文本分类预测")
test_input = st.text_area("请输入要预测的文本：", "天下万般兵刃，唯有过往伤人最深啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊")
if st.button("预测"):
    data = {"text": test_input}
    url = "http://127.0.0.1:5000/predict"
    start_time = time.time()

    try:
        response = requests.post(url, json=data)
        elapsed_time = (time.time() - start_time) * 1000
        if response.status_code == 200:
            result = response.json()
            st.success(f"预测结果：{result['pred_classs']}")
            st.info(f'请求耗时：{elapsed_time:.2f}ms')
        else:
            st.error(f"请求失败: {response.json()['error']}")
    except Exception as e:
        st.error(f'请求出错：{str(e)}')

st.write("请求确保Flask已经运行")
